<?php
namespace Modules\Admin\Validations;

use Phalcon\Validation\Validator\PresenceOf;

use Common\Models\AdminModel;

class AdminValidation extends \Phalcon\Validation {
  public function login($data) {
    $this -> rules('admin_name',[
      new PresenceOf([
        'message' => '请输入管理员账号!'
      ])
    ]);

    $this -> add('password',
      new PresenceOf([
        'message' => '请输入密码!'
      ])
    );

    $validationMessageGroup = $this -> validate($data);
    if($validationMessageGroup -> valid()) {
      return ['code' => -1, 'message' => $validationMessageGroup[0] -> getMessage()];
    }

    $findFirstAdmin = AdminModel::findFirst('admin_name = "' . $data['admin_name'] . '"');
    if(!$findFirstAdmin || !$this -> security -> checkHash($data['password'], $findFirstAdmin -> password)) {
      return ['code' => -1, 'message' => '账号或密码错误!'];
    }

    if($findFirstAdmin -> status != 0) {
      return ['code' => -1, 'message' => '管理员账号' . $this -> config -> admin_status[$findFirstAdmin -> status]];
    }

    return ['code' => 0, 'message' => '管理员登陆验证成功!', 'data' => ['admin' => $findFirstAdmin]];

  }
}
?>